package com.shuoyue.ycgk.ui.shop.contract;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.MyBanner;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public interface ShopGoodsContract {
    interface View extends BaseView {
        void setBaseType(List<Type> baseTypes);

        void setBooksList(ListWithPage<ShopItem> booksList);

        void setBanners(ArrayList<MyBanner> banners);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        /**
         *
         */
        public void getBaseModels() {

            apply(model.getBaseType(31))
                    .subscribe(new ApiSubscriber<Optional<List<Type>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Type>> listOptional) {
                            super.onNext(listOptional);
                            mView.setBaseType(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getBookList(int moduleId, int page) {
            apply(model.getShopList(moduleId, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<ShopItem>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<ShopItem>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.setBooksList(listWithPageOptional.getIncludeNull());
                        }
                    });
        }


        public void getBanners() {
            apply(model.getBanner())
                    .subscribe(new ApiSubscriber<Optional<List<MyBanner>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<MyBanner>> listOptional) {
                            super.onNext(listOptional);
                            mView.setBanners((ArrayList<MyBanner>) listOptional.getIncludeNull());
                        }
                    });

        }

    }

    class Model extends BaseModel {
        Observable<BaseResult<List<Type>>> getBaseType(int id) {
            return RetrofitClient.getInstance().getApi().moduleList(id);
        }

        Observable<BaseResult<ListWithPage<ShopItem>>> getShopList(int moduledId, int pageNum) {
            return RetrofitClient.getInstance().getApi().booksList(moduledId, pageNum);
        }

        Observable<BaseResult<List<MyBanner>>> getBanner() {
            return RetrofitClient.getInstance().getApi().getHomeBanners(0);
        }
    }
}
